<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>代理列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
    <style>
        .layui-table-body .layui-table-cell {
            height: 52px; line-height: 52px;
        }
        .layui-laypage .layui-laypage-curr .layui-laypage-em{ background-color: #1E9FFF; }
        .layui-form-checked[lay-skin=primary] i{border-color: #1E9FFF!important;background-color: #1E9FFF;color:#fff;}
        .layui-form-checkbox[lay-skin=primary]:hover i{border-color:#1E9FFF;color:#fff}
    </style>
</head>
<body>

<div class="layui-row">

    <div class="layui-col-md12">
        <div class="layui-card">

            <div lay-filter="listSearch" class="layui-form layui-card-header layuiadmin-card-header-auto" style="height:auto;padding:10px 15px;">

                <div class="layui-form-item">

                    <div class="layui-inline">
                        <input type="text" name="account" placeholder="请输入代理账户名称" autocomplete="off" class="layui-input">
                    </div>

                    <div class="layui-inline">
                        <select name="status" lay-search>
                            <option value="-1">账户状态</option>
                            <option value="1">正常</option>
                            <option value="0">禁用</option>
                        </select>
                    </div>

                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-admin" lay-submit lay-filter="LAY-list-front-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="LAY-reset">重置</button>
                    </div>

                </div>

            </div>

            <div class="layui-card-body">

                <div style="padding-bottom: 10px;">
                    <button class="layui-btn layui-btn-normal layuiadmin-btn-useradmin" data-type="add">添加</button>
                </div>

                <table id="LAY-list-manage" lay-filter="LAY-list-manage"></table>

                <script type="text/html" id="table-tool">

                    <p style="line-height: 50px;">

                        <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm" lay-event="AuthQuick">授权</a>

                        <a class="layui-btn layui-btn-primary layui-border-orange layui-btn-sm" lay-event="switchAccount">切换账号</a>
                        {{#  if(d.status == '1'){ }}
                        <a class="layui-btn layui-btn-warm layui-btn-sm" lay-event="disable">禁用</a>
                        {{#  } }}
                        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="resetPwd">重置密码</a>

                    </p>

                </script>

            </div>

        </div>
    </div>

</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>

    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table','admin'], function(){

        var $ = layui.$
            ,form = layui.form
            ,admin = layui.admin
            ,table = layui.table;

        //监听搜索
        form.on('submit(LAY-list-front-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('LAY-list-manage', {
                where: field
                ,page: {
                    curr: 1  // 将页码重置为第一页
                }
            });
        });

        form.on('submit(LAY-reset)', function(data){
            form.val("listSearch", {
                "account": "",
                "status": "-1"
            });
            form.render();
            table.reload('LAY-list-manage', {
                where: {"account": "", "status": "-1"}
                ,page: {
                    curr: 1  // 将页码重置为第一页
                }
            });
        });

        //书籍管理
        table.render({
            elem: '#LAY-list-manage'
            ,url: "{:url('Agent/list_data')}" //模拟接口
            ,autoSort: false
            , response: {
                statusCode: 1 //规定成功的状态码，本来是：0，现在统一改为1
            }
            ,cols: [[
                {field: 'id', width: 100, title: '代理ID', sort: true, align:'center'}
                ,{field: 'account', title: '账户名称', align:'center'}
                ,{field: 'state', title: '状态', align:'center'}
                ,{field: 'remarks', title: '备注', align:'center'}
                ,{field: 'quicks', title: '已授权App', align:'center'}
                ,{field: 'create_time', title: '创建时间', align:'center'}
                ,{title: '操作', width: 350, align:'center', fixed: 'right', toolbar: '#table-tool'}
            ]]
            ,page: true
            ,limit: 30
            ,height: 'full-220'
            ,text: '对不起，加载出现异常！'
        });

        //触发排序事件
        table.on('sort(LAY-list-manage)', function(obj){

            table.reload('LAY-list-manage', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    ,order: obj.type //排序方式
                }
            });

        });

        //监听工具条
        table.on('tool(LAY-list-manage)', function(obj){

            if(obj.event === 'switchAccount'){

                $.ajax({
                    url: "{:url('ManagerUser/switch_account')}",
                    type: "post",
                    dataType: "json",
                    data: {id:obj.data.id},
                    async: true,
                    success: function (rs) {
                        if (rs['code'] == 100) {
                            parent.location.href = '/';
                        } else {
                            layer.msg(rs['msg']);
                        }
                        return false;
                    }
                });

            }
            else if(obj.event === 'AuthQuick'){
                layer.open({
                    type: 2
                    ,title: obj.data.account + '-授权App'
                    ,offset: '15px'
                    ,content: '/agent/empowerApp.html?id=' + obj.data.id
                    ,area: ['615px', '500px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        layer.confirm('确认修改吗？授权后App将不能取消授权！', {
                            btn: ['确定', '取消'] //按钮
                        }, function () {

                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
                            //监听提交
                            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function (data) {
                                var field = data.field; //获取提交的字段
                                admin.req({
                                    url: '/agent/empowerApp'
                                    , type: "POST"
                                    , data: field
                                    , done: function (res) {
                                        layer.msg('设置成功', {icon: 1, time: 1000}, function () {
                                            table.reload('LAY-list-manage');
                                            layer.close(index); //关闭弹层
                                        });
                                    }, not_finish: function (res) {
                                        layer.msg(res.msg);
                                    }
                                });
                            });
                            submit.trigger('click');

                        }, function () {

                        });
                    }
                    ,success: function(layero, index){

                    }
                })
            }
            else if(obj.event === 'resetPwd'){
                layer.open({
                    type: 2
                    ,title: '重置密码'
                    ,content: "{:url('ManagerUser/reset_pwd')}?id="+obj.data.id
                    ,maxmin: true
                    ,area: ['390px', '300px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-reset-pwd-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){

                            $.ajax({
                                url:"{:url('ManagerUser/reset_pwd')}",
                                type:"post",
                                dataType: "json",
                                data:data.field,
                                async:true,
                                success: function(rs){
                                    if(rs['code'] == 100){

                                        layer.msg('密码重置成功',function () {
                                            layer.close(index); //关闭弹层
                                        });

                                    }else{
                                        layer.msg(rs['msg']);
                                    }
                                    return false;
                                }
                            });
                        });

                        submit.trigger('click');
                    }
                });
            }
            else if(obj.event === 'disable'){

                layer.open({
                    type: 2
                    ,title: '账号禁用'
                    ,content: "{:url('ManagerUser/disable')}?id="+obj.data.id
                    ,maxmin: true
                    ,area: ['390px', '300px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-disable-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){

                            $.ajax({
                                url:"{:url('ManagerUser/disable')}",
                                type:"post",
                                dataType: "json",
                                data:data.field,
                                async:true,
                                success: function(rs){
                                    if(rs['code'] == 100){

                                        layer.msg(rs['msg'],function () {
                                            layer.close(index); //关闭弹层
                                            table.reload('LAY-list-manage');
                                        });

                                    }else{
                                        layer.msg(rs['msg']);
                                    }
                                    return false;
                                }
                            });
                        });

                        submit.trigger('click');
                    }
                });

            }
        });

        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加代理'
                    ,content: "{:url('Agent/add')}"
                    ,maxmin: true
                    ,area: ['540px', '420px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-data-add-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){

                            $.ajax({
                                url:"{:url('Agent/add')}",
                                type:"post",
                                dataType: "json",
                                data:data.field,
                                async:true,
                                success: function(rs){
                                    if(rs['code'] == 100){

                                        layer.msg('代理添加成功',function () {
                                            layer.close(index); //关闭弹层
                                            table.reload('LAY-list-manage');
                                        });

                                    }else{
                                        layer.msg(rs['msg']);
                                    }
                                    return false;
                                }
                            });
                        });

                        submit.trigger('click');
                    }
                });
            }
        };

        $('.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>
</html>